import {_decorator, Component, Node, Vec3, lerp} from 'cc';
import {BaseInfo} from "db://assets/scripts/data/model/BaseInfo";
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {PoolManager} from "db://assets/scripts/common/PoolManager";
import {EffectManager} from "db://assets/scripts/common/EffectManager";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {GameState} from "db://assets/scripts/game/GameState";

const {ccclass, property} = _decorator;

/**
 * 360度散射脚本: 挂载每个球上,而非父节点上
 */
@ccclass('DispersionSurroundEffect')
export class DispersionSurroundEffect extends Component {
    /**
     * 敌人基本信息
     */
    public baseInfo: BaseInfo;
    /**
     * 技能信息
     */
    public skill: Skill;

    /**
     * 当前速度
     * @private
     */
    private currentSpeed: number = 0;
    /**
     * 目标速度
     * @private
     */
    private targetSpeed: number = 0;
    /**
     * 初始默认位置
     * @private
     */
    private originPosition: Vec3 = null!;
    /**
     * 初始默认角度
     * @private
     */
    private originEulerAngles: Vec3 = null!
    /**
     * 和玩家之间的向量差
     * @private
     */
    private offsetPosition: Vec3 = new Vec3();
    /**
     * 当前节点世界坐标
     * @private
     */
    private currentWorldPosition: Vec3 = new Vec3();
    /**
     * 箭节点超过玩家这个范围则隐藏
     * @private
     */
    private disappearRange: number = 20;
    /**
     * 箭的下次目标位置
     * @private
     */
    private targetWorldPosition: Vec3 = new Vec3();
    /**
     * 初始缩放大小
     * @private
     */
    private originScale: Vec3 = new Vec3();

    /**
     * 初始化
     */
    public init(skill: Skill, baseInfo: BaseInfo) {
        this.skill = skill;
        this.baseInfo = baseInfo;
        this.node.active = true;

        if (!this.originPosition) {
            this.originPosition = this.node.position.clone();
        }

        if (!this.originEulerAngles) {
            this.originEulerAngles = this.node.eulerAngles.clone();
        }

        this.node.setPosition(this.originPosition);
        this.node.eulerAngles.set(this.originEulerAngles);

        this.targetSpeed = skill.speed;
        this.currentSpeed = skill.speed * 0.5;

        EffectManager.getInstance().playParticle(this.node);

        AudioManager.getInstance().playSound(AudioPaths.energyBallSound);
    }

    /**
     * 击中玩家后隐藏
     *
     * @see Arrow
     */
    public hide() {
        if (!this.node.parent) {
            return;
        }

        this.node.active = false;

        //如果dispersionSurround组里所有的球都隐藏了则回收整个dispersion预制体
        let isAllHide = this.node.parent?.children.every((child: Node) => {
            return child.active === false;
        })

        if (isAllHide && this.node.parent) {
            PoolManager.getInstance().putNode(this.node.parent);
        }
    }

    update(deltaTime: number) {
        if (!this.node.parent
            || !GameManager.getInstance().gamePlayerProxy.getPlayerNode()
            || GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER
            || GameManager.getInstance().gameStateProxy.getGameState() === GameState.PAUSE) {
            return;
        }

        //朝forward方向飞行
        this.currentSpeed = lerp(this.targetSpeed, this.currentSpeed, 0.25);
        this.targetWorldPosition.set(0, 0, -deltaTime * this.currentSpeed);
        this.node.translate(this.targetWorldPosition, Node.NodeSpace.LOCAL);

        //超过玩家一定范围则隐藏
        this.currentWorldPosition.set(this.node.worldPosition);
        Vec3.subtract(this.offsetPosition, this.currentWorldPosition, GameManager.getInstance().gamePlayerProxy.getPlayerNode().worldPosition);
        if (this.offsetPosition && this.offsetPosition.length() >= this.disappearRange) {
            this.hide();
        }
    }
}